package com.qqt.csr.common.exception;

import java.text.MessageFormat;

/**
 *
 */
public class BaseException extends RuntimeException {

	private static final long serialVersionUID = 1L;

	private StatusCode statusCode = StatusCode.SUCCESS;

	private Integer code = null;

	private Object[] args;

	public int getCode() {
		return code != null ? code : statusCode.getCode();
	}

	public void setCode(StatusCode statusCode) {
		this.statusCode = statusCode;
	}

	public StatusCode getStatusCode() {
		return statusCode;
	}

	public BaseException(Integer code, String message) {
		this(message, null, false, false);
		this.code = code;
	}

	public BaseException(StatusCode code, String message) {
		this(message, null, false, false);
		this.statusCode = code;
	}

	public BaseException(StatusCode code, boolean format, Object... message) {
		this(formatMessage(format, code.getMessage(), message), null, false, false);
		this.statusCode = code;
	}

	public BaseException(StatusCode code) {
		this(code.getMessage(), null, false, false);
		this.statusCode = code;
	}

	public BaseException(StatusCode code, Throwable cause) {
		this(code.getMessage(), cause, false, false);
		this.statusCode = code;
	}

	public BaseException(String message) {
		this(message, null, false, false);
	}

	public BaseException(String message, Throwable cause) {
		this(message, cause, false, false);
	}
	public BaseException(String message, Throwable cause, boolean writableStackTrace) {
		this(message, cause, false, writableStackTrace);
	}

	public BaseException(Throwable cause) {
		this(null, cause, false, false);
	}

	public BaseException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
		super(message, cause, enableSuppression, writableStackTrace);
	}

	public Object[] getArgs() {
		return args;
	}

	public void setArgs(Object[] args) {
		this.args = args;
	}

	public static String formatMessage(boolean format, String message, Object... args) {
		message = MessageFormat.format(message, args);
		return message;
	}
	
}
